package com.burst.reptile.modular.zp.service.impl;

import java.util.Arrays;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.burst.reptile.common.utils.ToolUtil;
import com.burst.reptile.framework.aspectj.lang.enums.MsgType;
import com.burst.reptile.modular.zp.entity.ZpNotice;
import com.burst.reptile.modular.zp.mapper.ZpNoticeMapper;
import com.burst.reptile.modular.zp.service.IZpNoticeService;

import cn.hutool.core.util.StrUtil;

/**
 * <p>
 * 通知公告 服务实现类
 * </p>
 *
 * @author wujin
 * @since 2020-07-10
 */
@Service
public class ZpNoticeServiceImpl extends ServiceImpl<ZpNoticeMapper, ZpNotice> implements IZpNoticeService {

	@Override
	public Page<ZpNotice> pageList(Page<ZpNotice> page, ZpNotice notice) {
		String noticeType = notice.getNoticeType();
		Long userId = notice.getUserId();
		String sign = notice.getSign();
		Long relationUser = notice.getRelationUser();
		String noticeStatus = notice.getNoticeStatus();
		QueryWrapper<ZpNotice> queryWrapper = new QueryWrapper<>();
		if(StrUtil.isNotEmpty(noticeType)) { // 消息类型
			queryWrapper.eq("notice_type", noticeType);
		}else {
			queryWrapper.in("notice_type", Arrays.asList("1", "3", "4", "6"));
		}
		if(ToolUtil.isNotEmpty(userId)) { // 用户
			queryWrapper.eq("user_id", userId);
		}
		if(ToolUtil.isNotEmpty(relationUser)) { // 用户
			queryWrapper.and(i -> i.eq("relation_user", relationUser).or().isNull("relation_user"));
		}
		if(StrUtil.isNotEmpty(sign)) { // 是否已读
			queryWrapper.eq("sign", sign);
		}
		if(StrUtil.isNotEmpty(noticeStatus)) { // 状态
			queryWrapper.eq("notice_status", noticeStatus);
		}
		queryWrapper.orderByDesc("create_time");
		return this.page(page, queryWrapper);
	}

	@Override
	public ZpNotice selectNoticeById(Long noticeId) {
		return this.baseMapper.selectById(noticeId);
	}

	@Override
	@Transactional
	public int insertNotice(ZpNotice notice) {
		String noticeType = notice.getNoticeType();
		if(StrUtil.equals(noticeType, MsgType.GIVE.getIndex())) { // 点赞
			List<ZpNotice> selectList = this.baseMapper.selectList(new QueryWrapper<ZpNotice>()
					.eq("article_id", notice.getArticleId())
					.eq("notice_type", noticeType)
					.eq("user_id", notice.getUserId()));
			if(ToolUtil.isNotEmpty(selectList)) {
				selectList.forEach(i -> {
					this.baseMapper.remove(i.getMid());
				});
			}
		}else if(StrUtil.equals(noticeType, MsgType.COLLECTION.getIndex())) { // 收藏 
			List<ZpNotice> selectList = this.baseMapper.selectList(new QueryWrapper<ZpNotice>()
					.eq("article_id", notice.getArticleId())
					.eq("notice_type", noticeType)
					.eq("user_id", notice.getUserId()));
			if(ToolUtil.isNotEmpty(selectList)) {
				selectList.forEach(i -> {
					this.baseMapper.remove(i.getMid());
				});
			}
		}else if(StrUtil.equals(noticeType, MsgType.FANS.getIndex())) { // 粉丝
			List<ZpNotice> selectList = this.baseMapper.selectList(new QueryWrapper<ZpNotice>()
					.eq("relation_user", notice.getArticleId())
					.eq("notice_type", noticeType)
					.eq("user_id", notice.getUserId()));
			if(ToolUtil.isNotEmpty(selectList)) {
				selectList.forEach(i -> {
					this.baseMapper.remove(i.getMid());
				});
			}
		}
		return this.baseMapper.insert(notice);
	}

	@Override
	public int updateNotice(ZpNotice notice) {
		return this.baseMapper.updateById(notice);
	}

	@Override
	public int deleteNoticeByIds(Long[] noticeIds) {
		return this.baseMapper.deleteBatchIds(Arrays.asList(noticeIds));
	}

}
